/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package fr.miage.shopping.presentation;

import fr.miage.shopping.dao.traitements.CategorieDao;
import fr.miage.shopping.metier.entite.Categorie;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import java.util.List;
import javax.swing.JOptionPane;

/**
 *
 * @author MACAJ
 */
public class SelectionCategorie extends JFrame implements ActionListener{

    private JButton bOK=new JButton("OK");
    private JButton bAnnuler=new JButton("Annuler");
    private JLabel label=new JLabel("Choisissez une categorie ");
    private JComboBox categories;
    private ModificationCategorie fModification;
     public SelectionCategorie(){
        super("Selection catégorie");
        initFrame();
    }
    public void initFrame(){
        Container pane = this.getContentPane();

        JPanel panelNorth=new JPanel();
        panelNorth.setLayout(new FlowLayout());
        panelNorth.add(label);
        pane.add(panelNorth,BorderLayout.NORTH);

        JPanel panelCenter=new JPanel();
        categories=new JComboBox(getCategories());
        panelCenter.add(categories, BorderLayout.CENTER);
        pane.add(panelCenter,BorderLayout.CENTER);
        JPanel panelSouth=new JPanel();
        panelSouth.setLayout(new FlowLayout());
        this.bOK.addActionListener(this);
        this.bOK.setActionCommand("ok");
        panelSouth.add(bOK);
        this.bAnnuler.addActionListener(this);
        this.bAnnuler.setActionCommand("annuler");
        panelSouth.add(bAnnuler);
        pane.add(panelSouth,BorderLayout.SOUTH);
        this.pack();
        this.setVisible(true);
    }

    public Vector<String> getCategories(){
        List<Categorie> list=CategorieDao.selectAllCategorie();
        Vector<String> categoriesVector=new Vector<String>();
        categoriesVector.add("");
        for(int i = 0;i<list.size();i++){
            categoriesVector.add(list.get(i).getNomCate());
        }
        return categoriesVector;
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getActionCommand().equalsIgnoreCase("annuler")) {
            this.setVisible(false);

        }else {
           String nom=(String)categories.getSelectedItem();
           if(!"".equals(nom)){
               Categorie c = new Categorie();
               c.setNomCate(nom);
               Categorie categorie=CategorieDao.selectCategorieByNom(c);
               fModification=new ModificationCategorie(categorie);
               this.setVisible(false);
               fModification.setVisible(true);
           } else {
               JOptionPane.showMessageDialog(null,"Aucune catégorie n'a été sélectionnée","Erreur formulaire",JOptionPane.INFORMATION_MESSAGE);

           }
           

        }
    }
}
